package so.ifl.servcie.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import so.ifl.commons.tools.StringHelper;
import so.ifl.dao.ReportInfoDao;
import so.ifl.servcie.ReportService;
import so.ifl.vo.ReportVo;

/**
 *create by aaron.lu
 *create at 2012-5-3 下午2:38:24
 *<mailto: atyuan.lu@gmail.com>
 **/
@Service("reportService")
public class ReportServiceImpl implements ReportService{

	@Autowired
	private ReportInfoDao reportInfoDao;
	
	/**
	 * 创建报表<通过学员进度>
	 * create by aaron.lu
	 * create at 2012-5-3 下午10:19:08
	 * <mailto: atyuan.lu@gmail.com>
	 * @param comapnyId
	 * @param reduce
	 * @param add
	 * @param change
	 *
	 */
	public void createReport(Integer comapnyId, String reduce, String add, Float change){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("company_id", comapnyId);
		
		if (StringHelper.isNotEmpty(reduce)) {
			params.put("report_key_", reduce);//查需要减量的报表		
			
			List<ReportVo> reduce_reports = reportInfoDao.queryReport(params);
			
			if (null != reduce_reports && reduce_reports.size() > 0) {
				//如果存在需要减量的报表
				ReportVo reduce_report = reduce_reports.get(0);
				if (reduce_report.getReportSum().floatValue() > change.floatValue()) {
					//如果减少的少于原报表数额
					reduce_report.setReportSum(reduce_report.getReportSum().floatValue() - change.floatValue());
				}else {
					//如果减少的大于原报表数额
					reduce_report.setReportSum(new Float(0));
				}	
				reportInfoDao.updateReport(reduce_report);
			}else{
				//如果不存在需要减量的报表
				Map<String, Object> map = getMapping();
				ReportVo reportVo = new ReportVo(comapnyId, map.get(reduce).toString(), "progress", reduce, map.get(reduce).toString(), new Float(0), null, null, null);
				reportInfoDao.insertReport(reportVo);			
			}
		}
		
		if (StringHelper.isNotEmpty(add)) {
			params.put("report_key_", add);//查需增量的报表
			
			List<ReportVo> add_reports = reportInfoDao.queryReport(params);
			
			if (null != add_reports && add_reports.size() > 0) {
				//如果存在需要增量的报表
				ReportVo add_report = add_reports.get(0);
				add_report.setReportSum(add_report.getReportSum().floatValue() + change.floatValue());
				reportInfoDao.updateReport(add_report);
			}else{
				//如果不存在需要增量的报表
				Map<String, Object> map = getMapping();
				ReportVo reportVo = new ReportVo(comapnyId, map.get(reduce).toString(), "progress", reduce, map.get(reduce).toString(), new Float(0), null, null, null);
				reportInfoDao.insertReport(reportVo);				
			}	
		}
			
	}
	
	/**
	 * 创建报表<通过来源>
	 * create by aaron.lu
	 * create at 2012-5-3 下午10:23:24
	 * <mailto: atyuan.lu@gmail.com>
	 * @param comapnyId
	 * @param reduce
	 * @param add
	 * @param change
	 *
	 */
	public void createReportWithOffice(Integer comapnyId, String reduce, String add, Float change){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("company_id", comapnyId);
		
		if (StringHelper.isNotEmpty(reduce)) {
			params.put("report_key_", reduce);// 查需要减量的报表
			

			List<ReportVo> reduce_reports = reportInfoDao.queryReport(params);

			if (null != reduce_reports && reduce_reports.size() > 0) {
				// 如果存在需要减量的报表
				ReportVo reduce_report = reduce_reports.get(0);
				if (reduce_report.getReportSum().floatValue() > change.floatValue()) {
					// 如果减少的少于原报表数额
					reduce_report.setReportSum(reduce_report.getReportSum().floatValue() - change.floatValue());
				} else {
					// 如果减少的大于原报表数额
					reduce_report.setReportSum(new Float(0));
				}
				reportInfoDao.updateReport(reduce_report);
			} else {
				// 如果不存在需要减量的报表
				ReportVo reportVo = new ReportVo(comapnyId, reduce, "office", reduce, reduce, new Float(0), null, null, null);
				reportInfoDao.insertReport(reportVo);
			}
		}
		
		if (StringHelper.isNotEmpty(add)) {
			params.put("report_key_", add);// 查需增量的报表

			List<ReportVo> add_reports = reportInfoDao.queryReport(params);

			if (null != add_reports && add_reports.size() > 0) {
				// 如果存在需要增量的报表
				ReportVo add_report = add_reports.get(0);
				add_report.setReportSum(add_report.getReportSum().floatValue() + change.floatValue());
				reportInfoDao.updateReport(add_report);
			} else {
				// 如果不存在需要增量的报表
				ReportVo reportVo = new ReportVo(comapnyId, reduce, "office", reduce, reduce, new Float(0), null, null, null);
				reportInfoDao.insertReport(reportVo);
			}
		}
		
	}
	
	/**
	 * 通过各种状态的学员
	 * create by aaron.lu
	 * create at 2012-5-3 下午11:13:23
	 * <mailto: atyuan.lu@gmail.com>
	 * @return
	 *
	 */
	public List<Map<String, Object>> countStudentByProgress(){
		return reportInfoDao.countStudentByProgress();
	}
	
	/**
	 * 统计学员
	 * create by aaron.lu
	 * create at 2012-5-3 下午11:54:16
	 * <mailto: atyuan.lu@gmail.com>
	 * @param officeId
	 * @return
	 *
	 */
	public int countStudentByOffice(Integer officeId){
		Map<String, Object> params = new HashMap<String, Object>();
		if (null != officeId) {
			params.put("officeId", officeId);
		}
		return reportInfoDao.countStudentByOffice(params);
	}
	
	/**
	 * 报表统计
	 * create by aaron.lu
	 * create at 2012-5-5 上午2:18:03
	 * <mailto: atyuan.lu@gmail.com>
	 * @param officeId
	 * @return
	 *
	 */
	public List<Map<String, Object>> countStudent(Integer officeId){
		Map<String, Object> params = new HashMap<String, Object>();
		if (null != officeId) {
			params.put("office_id_", officeId);
			return reportInfoDao.countStudent(params);
		}else{
			return reportInfoDao.countStudentByProgress();
		}
		
	}
	
	/**
	 * 获得配置表
	 * create by aaron.lu
	 * create at 2012-5-3 下午10:10:41
	 * <mailto: atyuan.lu@gmail.com>
	 * @return
	 *
	 */
	private Map<String, Object> getMapping(){
		Map<String, Object> mapping = new HashMap<String, Object>();
		mapping.put("progess1", "收表");
		mapping.put("progess2", "待考桩");
		mapping.put("progess3", "科目一不及格");
		mapping.put("progess4", "待场地");
		mapping.put("progess5", "桩试不及格");
		mapping.put("progess6", "待长途");
		mapping.put("progess7", "场地不及格");
		mapping.put("progess8", "待安全课");
		mapping.put("progess9", "待科目三");
		mapping.put("progess10", "科目三不及格");
		return mapping;
	}
	
}
